package com.zhang.modules.auth.service.impl;

import com.zhang.common.jpa.service.impl.BaseServiceImpl;
import com.zhang.modules.api.entity.Api;
import com.zhang.modules.api.service.IApiService;
import com.zhang.modules.auth.eneity.Permission;
import com.zhang.modules.auth.eneity.dto.PermissionDto;
import com.zhang.modules.auth.rep.PermissionRep;
import com.zhang.modules.auth.service.IPermissionService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.crypto.hash.HashService;
import org.springframework.beans.BeanUtils;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;


@Service
public class PermissionService extends BaseServiceImpl<PermissionRep, Permission,String> implements IPermissionService {

    private IApiService apiService;

    public PermissionService(IApiService apiService) {
        this.apiService = apiService;
    }

    /**
     * 根据角色id获取权限列表
     * @param roleId
     * @return
     */
    private List<Permission> findAllByRoleId(String roleId){
       return getRep().findAllByRoleId(roleId);
    }


    /**
     * 格式化{@link PermissionService#findAllByRoleId(String)}的结果
     */
    public Set<String> findStrByRoleId(String roleId){
        //判空处理
        if (StringUtils.isEmpty(roleId)) return null;
        Set<String> result = new HashSet<>();
        List<Permission> permissionList = findAllByRoleId(roleId);
        //添加权限code到结果集
        permissionList.forEach(item ->{
            result.add(item.getCode());
        });

        return result;
    }

    /**
     * 清空权限表
     */
    public long cleanUp(){
        return getRep().deleteByIdNotNull();
    }

    /**
     * @return 返回权限的树形结构 其实就两层
     */
    @Override
    public List<PermissionDto> Alltree() {
        //权限按照pid分组
        List<Permission> allPermission = getAllLogic();
        Map<String, List<Permission>> apis = allPermission
                .stream()
                .collect(Collectors.
                        groupingBy(
                                Permission::getPid,
                                Collectors.toList()));
        //权限转换为dto列表
        ArrayList<PermissionDto> result = new ArrayList<>();
        apis.keySet().forEach(pid ->{
            Api apiGroup = apiService.get(pid);
            PermissionDto dto = new PermissionDto(apiGroup.getId(),
                                                  apiGroup.getName(),
                                                  apiGroup.getCode(),
                                                  convert2Dto(apis.get(pid)));
            result.add(dto);
        });


        return result;
    }

    /**
     * 转换
     */
    private List<PermissionDto> convert2Dto(List<Permission> permissions){
        ArrayList<PermissionDto> result = new ArrayList<>();

        permissions.forEach(item ->{
            PermissionDto permissionDto = new PermissionDto();
            BeanUtils.copyProperties(item,permissionDto);
            result.add(permissionDto);
        });

        return result;
    }


}
